﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TopVisionVideoStream;
using VideoStream.Common;
using VideoStream.Model;
using VideoStream.Net;

namespace VideoStream.Windows
{
    public partial class Windows_Startup : WindowsBase
    {
        #region Filds
        public static Windows_Startup Instance;

        private WindowsBase winMain;

        private NetworkManager networkManager;

        #endregion

        public Windows_Startup()
        {
            if (Instance == null)
            {
                Instance = this;
            }
            else if (Instance != this)
            {
                Console.WriteLine("Singleton err : " + this.Name);

                Hide();
                Close();
                return;
            }

            InitializeComponent();
            InitUI();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            InitEvent();
            UpdateUI();
            LoadMain(1000);
            GetUserInfo();
            networkManager = new NetworkManager();
            //CommonFun.Netchecking(null, 1000);
        }

        protected override void OnClosed(EventArgs e)
        {
            networkManager?.Dispose();
            base.OnClosed(e);
        }

        public override void InitEvent()
        {
            base.InitEvent();

            InitTVVS(this);
        }

        public override void InitUI()
        {
            base.InitUI();
            this.ShowInTaskbar = false;
            this.Width = 400;
            this.Height = 200;
            this.ShowTitle = false;
        }

        public override void UpdateUI()
        {
            base.UpdateUI();
            this.BackColor = TVColor.Color_HighLight;

            lab_AppTitle.Text = TVString.STR_AppTitle;
            lab_AppTitle.ForeColor = TVColor.Color_White;
            lab_AppTitle.Font = TVFont.DefaultFont26;

            lab_Version.ForeColor = TVColor.Color_White;
            lab_Version.Text = CommonFun.AssemblyVersion;
        }

        private void InitTVVS(Control context)
        {
#if DEBUG
            TVVS.IsDebug = true;
#else
            TVVS.IsDebug = false;
#endif
            var ret = TVVS.Init(context);
            if (!ret)
            {
                MessageBox.Show(this, TVString.STR_StartupFailed, TVString.STR_Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Close();
            }

            TVVS.ComConfEnable = false;
            TVVS.VideoListEnable = false;
        }

        private async void LoadMain(int delay)
        {
            await Task.Delay(delay);
            this.Hide();
            winMain = new Windows_Main();
            winMain.Show();
        }


        private void GetUserInfo()
        {
            var userInfo = CommonFun.GetUserInfo();
            if (userInfo == null)
            {
                ApplicationData.KeepPassword = false;
            }
            else
            {
                ApplicationData.Account = userInfo[0];
                ApplicationData.Password = userInfo[1];
                ApplicationData.KeepPassword = !string.IsNullOrEmpty(ApplicationData.Password);
            }
        }
    }
}
